package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: HuoXu
 * @Date: 2023/07/19/19:13
 * @Description: 快乐跳跳虎
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 新增菜品和口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        //将传输对象得dto得属性值拷贝到Dish类中
        BeanUtils.copyProperties(dishDTO,dish);
        //补齐数据库字段
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.addDish(dish);
        //获取插入菜品得主键值id
        Long dishId=dish.getId();
        //向菜品口味添加数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if(dishFlavors!=null && dishFlavors.size()>0){
            //遍历集合设置集合里dish_id的值
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.addDishFlavor(dishFlavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryByPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.queryByPage(dishPageQueryDTO);
        long total=page.getTotal();
        List<DishVO> records=page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void deleteDish(List<Long> ids) {
        //根据id进行判断对应的菜品是否起售，如果是在售状态则不允许删除
        for (Long id : ids) {
            Dish dish=dishMapper.queryById(id);
            if(dish.getStatus()== StatusConstant.ENABLE)
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断选择的菜品中是否含有已售的菜品，如果有则不允许删除
        List<Long> setMealIds=setMealDishMapper.queryByDishId(ids);
        if(setMealIds!=null&&setMealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品数据
        dishMapper.deleteDish(ids);
        //删除菜品对应的口味数据
        dishFlavorMapper.deleteFlavorByDishId(ids);
    }

    /**
     * 根据分类id查询菜品
     * @param dish
     * @return
     */
    @Override
    public List<Dish> queryByClassificationId(Dish dish) {
        //查询的数据为多条并不唯一所以使用列表进行封装
        List<Dish> dishes=dishMapper.queryByClassificationId(dish);
        return dishes;
    }

    /**
     * 菜品的起售与停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        dishMapper.startOrStop(status,id);
    }

    /**
     * 根据id进行菜品查询
     * @param id
     * @return
     */
    @Override
    public DishVO queryById(Long id) {
        DishVO dishvo=dishMapper.dataEcho(id);
        List<DishFlavor> dishFlavorList=dishFlavorMapper.queryByDishId(id);
        dishvo.setFlavors(dishFlavorList);
        return dishvo;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    public void editDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //补齐数据库中的字段
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        //修改菜品表中的数据
        dishMapper.editDish(dish);

        //先删除原有的口味数据
        List<Long> dishId = new ArrayList<>();
        dishId.add(dishDTO.getId());
        dishFlavorMapper.deleteFlavorByDishId(dishId);

        //向菜品口味添加新的口味数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            //遍历集合设置集合里dish_id的值
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.addDishFlavor(dishFlavors);
        }
    }

    //以下是客户端查询数据的接口
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.queryByClassificationId(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.queryByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
